/*
 * @Author: anxia.ack anchenkai@come-future.com
 * @Date: 2025-03-22 15:59:47
 * @LastEditors: anxia.ack anchenkai@come-future.com
 * @LastEditTime: 2025-04-16 15:19:20
 * @FilePath: /free-learning-front/vite.config.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import { fileURLToPath } from "url";
import { dirname, resolve } from "path";

// ESM兼容方式获取__dirname
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [vue()],
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    server: {
      host: "0.0.0.0",
      port: 80,
      open: false,
      proxy: {
        // 代理 /dev-api 的请求
        [env.VITE_APP_BASE_API]: {
          changeOrigin: true,
          // 代理目标地址：https://api.youlai.tech
          target: env.VITE_APP_API_URL,
          rewrite: (path) =>
            path.replace(new RegExp("^" + env.VITE_APP_BASE_API), ""),
        },
      },
    },
    build: {
      // 确保正确处理组件
      rollupOptions: {
        output: {
          manualChunks: {
            vue: ["vue", "vue-router"],
            components: [
              "./src/components/Footer.vue",
              "./src/components/Navbar.vue",
            ],
          },
        },
      },
      // 启用源码映射
      sourcemap: true,
      // 确保正确处理CSS
      cssCodeSplit: true,
    },
  };
});
